import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.net.URLDecoder;

public class ViewCartServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.println("<!DOCTYPE html>");
        out.println("<html lang=\"zh-CN\">");
        out.println("<head>");
        out.println("<meta charset=\"UTF-8\">");
        out.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
        out.println("<title>我的购物车</title>");
        out.println("<link rel=\"stylesheet\" href=\"style.css\">");
        out.println("</head>");
        out.println("<body>");
        out.println("<div class=\"container\">");
        out.println("<h1>我的购物车</h1>");

        // 获取购物车Cookie
        Cookie cartCookie = getCartCookie(request);
        String cartJson = URLDecoder.decode(cartCookie.getValue(), "UTF-8");

        // 解析JSON购物车数据
        Gson gson = new Gson();
        Type cartType = new TypeToken<List<CartItem>>(){}.getType();
        List<CartItem> cartItems = gson.fromJson(cartJson, cartType);

        if (cartItems != null && !cartItems.isEmpty()) {
            // 计算总价和商品数量
            double total = 0.0;
            int itemCount = 0;

            out.println("<table>");
            out.println("<tr>");
            out.println("<th>商品名称</th>");
            out.println("<th>单价</th>");
            out.println("<th>数量</th>");
            out.println("<th>小计</th>");
            out.println("<th>操作</th>");
            out.println("</tr>");

            for (CartItem item : cartItems) {
                double subtotal = item.getSubtotal();
                total += subtotal;
                itemCount += item.getQuantity();

                out.println("<tr>");
                out.println("<td>" + item.getProductName() + "</td>");
                out.println("<td>¥" + String.format("%.2f", item.getPrice()) + "</td>");
                out.println("<td>" + item.getQuantity() + "</td>");
                out.println("<td>¥" + String.format("%.2f", subtotal) + "</td>");
                out.println("<td>");
                out.println("<form action=\"remove-from-cart\" method=\"post\" style=\"display:inline;\">");
                out.println("<input type=\"hidden\" name=\"productId\" value=\"" + item.getProductId() + "\">");
                out.println("<button type=\"submit\" class=\"btn btn-remove\">移除</button>");
                out.println("</form>");
                out.println("</td>");
                out.println("</tr>");
            }

            out.println("<tr class=\"total-row\">");
            out.println("<td colspan=\"3\">总计 (" + itemCount + " 件商品)</td>");
            out.println("<td colspan=\"2\">¥" + String.format("%.2f", total) + "</td>");
            out.println("</tr>");
            out.println("</table>");

            out.println("<div class=\"actions-center\">");
            out.println("<a href=\"index.html\" class=\"btn btn-continue\">继续购物</a>");
            out.println("<a href=\"#\" class=\"btn btn-checkout\">结算购物车</a>");
            out.println("</div>");
        } else {
            out.println("<div class=\"empty-cart\">");
            out.println("<h3>您的购物车是空的</h3>");
            out.println("<p>快去挑选一些商品吧！</p>");
            out.println("<a href=\"index.html\" class=\"btn btn-continue\">返回购物</a>");
            out.println("</div>");
        }

        out.println("</div>");
        out.println("</body>");
        out.println("</html>");
    }

    private Cookie getCartCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("cart".equals(cookie.getName())) {
                    return cookie;
                }
            }
        }
        return new Cookie("cart", "[]");
    }
}